package com.xzx.shop_front_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzx.shop_front_api.entity.Order;
import com.xzx.shop_front_api.mapper.OrderMapper;
import com.xzx.shop_front_api.service.OrderService;
import com.xzx.shop_front_api.utils.InstantConverter;
import com.xzx.shop_front_api.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private Order order;
    @Override
    public int insertOrder(Order order) {
        return orderMapper.insert(order);
    }

    @Override
    public int updateOrderStatusPayStatus(String outTradeNo,
                                          int payStatus,
                                          int payType,
                                          String gmt_payment,
                                          int status,
                                          String tradeNo) throws ParseException {
        order.setPayStatus(payStatus);
        order.setStatus(status);
        order.setPayTime(InstantConverter.toInteger(gmt_payment));
        order.setPayType(payType);
        order.setUpdateTime(Tool.getCurrentTime());
        order.setTradeNo(tradeNo);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("out_trade_no", outTradeNo);
        return orderMapper.update(order,wrapper);
    }

    @Override
    public Order selectOrderByOrderSn(String outTradeNo) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("out_trade_no", outTradeNo);
        return orderMapper.selectOne(wrapper);
    }
}
